var orderId;
$(function () {
	orderId = $.getUrlParam("id");
	orderNum = $.getUrlParam("orderNum");
	//货物类型的下拉列表加载
	$.ajax({
	    url: "../cargotype/getAllCargoTypeInfo.htm",
	    cache: false,
	    type: "get",
	    dataType: "json",
	    async: false,
	    success: function (result) {
	        if (result.status == 0) {
	            var data = result.data;
	            addOptions(data, "#cargoType");
	        } 
	    }
	});
	
	//如果orderId不为null，下面是使用orderNum进行回显数据
    if ('null' != orderId){
    	echoOrderInfo();
    }
	
    //自定义表单验证
    $('#editOrderForm').validationEngine('attach', {
        validationEventTrigger : 'blur',
        promptPosition: 'topRight',
        autoPositionUpdate: true,	//自动调整提示层的位置
        scroll: false,
        focusFirstField : true,
        onValidationComplete : function (form, valid) {
            if (valid){
                if (orderId == 'null') {
                	addOrderInfo();
                } else {
                	updateOrderInfo();
                }
            }
        }
    });
});

//关闭窗口
function closeForm() {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

//点击编辑回显数据
function echoOrderInfo(){
    $.ajax({
        url: "../order/echoOrderInfo.htm",
        cache: false,
        async : false,
        type: "post",
        dataType: "json",
	    data: {"orderNum": orderNum},
        success: function (result) {
            if (result.status == 0) {
            	$('#orderNum').val(result.data.orderNum);
                var sender = result.data.sender;
                $('#senderName').val(sender.senderName);
                $('#senderIdcard').val(sender.senderIdcard);
                $('#senderPhone').val(sender.senderPhone);
                $('#senderArea').val(sender.senderArea);
                $('#senderAddr').val(sender.senderAddr);
                var reciver = result.data.reciver;
                $('#reciverName').val(reciver.reciverName);
                $('#reciverIdcard').val(reciver.reciverIdcard);
                $('#reciverPhone').val(reciver.reciverPhone);
                $('#reciverArea').val(reciver.reciverArea);
                $('#reciverAddr').val(reciver.reciverAddr);
                var cargo = result.data.cargo;
                $('#cargoName').val(cargo.cargoName);
                $('#cargoAmount').val(cargo.cargoAmount);
                $('#cargoLong').val(cargo.cargoLong);
                $('#cargoWeight').val(cargo.cargoWeight);
                $('#cargoHeight').val(cargo.cargoHeight);
                $('#cargoWide').val(cargo.cargoWide);
                $('#cargoType').val(cargo.cargoType);
                $('#orderCod').val(result.data.orderCod);
                $('#orderAddedFee').val(result.data.orderAddedFee);
                $('#orderNotes').val(result.data.orderNotes);
                return;
            }
        }
    });
}

//更新订单详情
function updateOrderInfo() {
    $.ajax({
        url: "../order/modifyOrderInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editOrderForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
                parent.layer.msg(result.message, {icon: 1});
                //关闭弹出窗之前，刷新列表页面的数据
                parent.$table.bootstrapTable('refresh', {url: '../order/getAllPayFailOrderPage.htm'});//调用父方法刷新
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            } else {
                layer.msg(result.message, {icon: 2});
            }
        }
    });
}


//增加订单信息
function addOrderInfo() {
    $.ajax({
        url: "../order/addToOrderInfo.htm",
        cache: false,
        type: "post",
        dataType: "json",
        data: $("#editOrderForm").serialize(),
        beforeSend:function(){var index = layer.load(1, { shade: [0.8,'#ddd']});},
        complete:function(){var index = layer.closeAll("loading");},
        success: function (result) {
            if (result.status == 0) {
            	parent.layer.msg(result.message, {icon: 1});
                parent.$table.bootstrapTable('refresh', {url: '../order/getAllPayFailOrderPage.htm'});
            } else {
                parent.layer.msg(result.message, {icon: 2});
            }
            closeForm();
        }
    });
}

//追加到option中
function addOptions(data,selectId){
	for(var i=0;i<data.length;i++){
		$(selectId).append('<option value="' + data[i].cargoTypeId + '">' + data[i].cargoTypeName + '</option>');
	}
}

